import requests;
import re;
import json;

from utils.Utils import printFetchUrl

class FetchSearchHtml:
    def __init__(self, keyWord):
        self.keyWord = keyWord;

    def fetchHtml(self, index):
        url = "https://www.alibaba.com/trade/search?fsb=y&IndexArea=product_en&CatId=&SearchText={}&viewtype=&tab=&page={}".format(self.keyWord, index);
        printFetchUrl(url);
        req = requests.get(url);
        return req.text;

    def getWaterfallCtrPageId(self, html):
        searchObject = re.search("""window.__APLUS_ABRATE__\['p4p_id'\] = \"(.*)\"""", html, re.M | re.I);
        if searchObject:
            print("find waterfallCtrPageId:", searchObject.group(1))
            return searchObject.group(1);
        else:
            return '';

    def fetchOfferDictionary(self, index, waterfallCtrPageId):
        url = "https://open-s.alibaba.com/openservice/galleryProductOfferResultViewService?appName=magellan" \
              "&appKey=a5m1ismomeptugvfmkkjnwwqnwyrhpb1&staticParam=&SearchText={}&IndexArea=product_en&page={}".format(self.keyWord, index);
        if(waterfallCtrPageId):
            url += "&waterfallCtrPageId={}&waterfallReqCount=1&asyncLoadIndex=2&asyncLoad=true".format(waterfallCtrPageId);
        printFetchUrl(url);
        req = requests.get(url);

        jsonObject = json.loads(req.text);
        offerList = jsonObject['data']['offerList'];
        print("offerList size:", len(offerList))
        return offerList;